﻿#ifndef __saas_string_dumper_h__
#define __saas_string_dumper_h__

#include "thisinc.h"
#include <hash_set>
#include <stdint.h>

typedef size_t ulong_t;
typedef unsigned char	uchar_t;
typedef const uchar_t	cuchar_t;
typedef const uint16_t	cwchar_t;

inline long is_mbs_end( cuchar_t* pc )
{
	if( 0 == *pc || 0xA == *pc || 0xD == *pc )
		return true;
	return false;
};

inline long is_mbs_asc( cuchar_t* pc ) 
{
	if( 0x20 <= *pc && 0x7F > *pc ) return true; 
	if( '\t' == *pc ) return true;
	return false;
};

static int mb_cmp( const void * l, const void* r )
{
	return (*(uint16_t*)l) - (*(uint16_t*)r);
}

inline long is_uni_end( cwchar_t* pc ) 
{
	if( 0 == *pc || 0xA == *pc || 0xD == *pc )
		return true;
	return false;
};

inline long is_uni_asc( cwchar_t* pc ) 
{
	if( 0x20 <= *pc && 0x7F > *pc ) return true;
	if( '\t' == *pc ) return true;
	return false;
};



class string_dumper
{
protected:
    size_t						m_minl;
    size_t						m_maxl;
    size_t						m_dumpedc;
    char*						m_tmp_mbs;

public:

	string_dumper();
    ~string_dumper();

    inline size_t count(){ return m_dumpedc; };

    bool prepare( size_t minl, size_t maxl );
    bool dump( size_t ofsbase, cuchar_t* buffer, size_t size );
	void complete();

    virtual bool on_discover_string( size_t raw_ofs, char* str, size_t cc, size_t crc, bool utf16 );
	virtual void DoSleep();

	bool dump_mbs( size_t ofsbase, cuchar_t* range, cuchar_t* reof );
	bool dump_unis( size_t ofsbase, cuchar_t* range, cuchar_t* reof );

protected:

	void __sleep();

    bool internal_on_discover_string( size_t raw_ofs, cuchar_t* str, size_t cc, bool utf16 );

    /* MULTI BYTE STRING */

    // 返回可能的串结束点
    cuchar_t*  find_mbs_range_end( cuchar_t* rng_sp, cuchar_t* rng_ep );
    // 返回去空格和制表符的串结束点
    cuchar_t*  trim_mbs_end( cuchar_t* str_ep, cuchar_t* rng_sp );
    // 从串结束点向前扫描找到一个有效的串开头
    cuchar_t*  find_mbs( cuchar_t* str_ep, cuchar_t* rng_sp, size_t& cc );
    // 返回去空格和制表符的串开头
    cuchar_t*  trim_mbs_start( cuchar_t* str_sp, size_t& cc );

    /* UNICODE STRING */

    // 返回可能的串结束点
    cwchar_t*  find_unis_range_end( cwchar_t* rng_sp, cuchar_t* rng_ep );
    // 返回去空格和制表符的串结束点
    cwchar_t*  trim_unis_end( cwchar_t* str_ep, cwchar_t* rng_sp );
    // 从串结束点向前扫描找到一个有效的串开头
    cwchar_t*  find_unis( cwchar_t* str_ep, cwchar_t* rng_sp, size_t& cc );
    // 返回去空格和制表符的串开头
    cwchar_t*  trim_unis_start( cwchar_t* str_sp, size_t& cc );
};


#endif  // __saas_string_dumper_h__